package com.xiaoshuidi.cloud.module.report.service.datamanage;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.module.report.controller.datamanage.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.datamanage.BusinessDataMapper;
import com.xiaoshuidi.cloud.module.report.utils.ApartmentUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusinessDataServiceImpl implements BusinessDataService{

    @Resource
    private BusinessDataMapper businessDataMapper;
    @Resource
    private ApartmentUtils apartmentUtils;


    /**
     * 在租房源总览
     *
     * @param apartmentIds
     * @param condition
     * @return
     */
    @Override
    public OverviewOfHouseRespVO overviewOfHouse(List<Long> apartmentIds, String condition) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        OverviewOfHouseRespVO respVO = businessDataMapper.overviewOfHouse(apartmentIds, condition);
        if(ObjectUtil.isNotEmpty(respVO.getTotalNum()) && respVO.getTotalNum()>0){
            respVO.setRentedRate(getRate(respVO.getRentedNum(),respVO.getTotalNum()));
            respVO.setScheduledRate(getRate(respVO.getScheduledNum(),respVO.getTotalNum()));
            respVO.setEmptyRate(getRate(respVO.getEmptyNum(),respVO.getTotalNum()));
            respVO.setOtherRate(getRate(respVO.getOtherNum(),respVO.getTotalNum()));
        }
        return respVO;
    }

    /**
     * 在租房源出租率统计
     * @param apartmentIds
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public RentalStatisticsRespVO rentalStatistics(List<Long> apartmentIds, LocalDate startDate, LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        String startDateStr = LocalDateTimeUtil.format(startDate, "yyyy-MM-dd 00:00:00");
        String endDateStr = LocalDateTimeUtil.format(endDate, "yyyy-MM-dd 23:59:59");
        List<RentalStatistics> rentalStatisticsList = businessDataMapper.rentalStatistics(apartmentIds, startDateStr, endDateStr);
        // 当前出租率
        OverviewOfHouseRespVO overviewOfHouseRespVO = overviewOfHouse(apartmentIds, null);
        return new RentalStatisticsRespVO().setRentalStatisticsList(rentalStatisticsList).setRentalRate(overviewOfHouseRespVO.getRentedRate());
    }

    /**
     * 空置房源变化趋势
     * @param apartmentIds 门店id
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Override
    public List<EmptyRoomChangeTrendRespVO> emptyRoomChangeTrend(List<Long> apartmentIds, LocalDate startDate, LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        return businessDataMapper.emptyRoomChangeTrend(apartmentIds, startDate, endDate);

    }

    /**
     * 签约总览
     * @param apartmentIds
     * @return
     */
    @Override
    public SigningOverviewRespVO signingOverview(List<Long> apartmentIds) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        return businessDataMapper.signingOverview(apartmentIds);
    }

    /**
     * 签约人员排名
     * @param apartmentIds 门店id
     * @param rankingMode 排名方式
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Override
    public List<RankingOfContractedRespVO> rankingOfContractedPersonnel(List<Long> apartmentIds, @NonNull String rankingMode,@NonNull LocalDate startDate,@NonNull LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        return businessDataMapper.rankingOfContractedPersonnel(apartmentIds, rankingMode, startDate, endDate);
    }

    /**
     * 门店签约排名
     * @param apartmentIds 门店id
     * @param rankingMode 排名方式
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @Override
    public List<RankingOfContractedRespVO> rankingOfContractedApartment(List<Long> apartmentIds, String rankingMode, LocalDate startDate, LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        return businessDataMapper.rankingOfContractedApartment(apartmentIds, rankingMode, startDate, endDate);    }


    /**
     * 合同到期数
     * @param apartmentIds
     * @return
     */
    @Override
    public List<ContractExpirationNumRespVO> contractExpirationNum(List<Long> apartmentIds) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        List<ContractExpirationNumRespVO> contractExpirationNumRespVOS = businessDataMapper.contractExpirationNum(apartmentIds);
        List<String> yearMonthList = getYearMonthList(LocalDate.now(), 12);
        return yearMonthList.stream().map(yearMonth -> {
            ContractExpirationNumRespVO respVO = new ContractExpirationNumRespVO();
            respVO.setAnalysisTime(yearMonth);
            contractExpirationNumRespVOS.stream().filter(p -> yearMonth.equals(p.getAnalysisTime())).findFirst().ifPresent(f -> {
                respVO.setNum(f.getNum());
            });
            return respVO;
        }).collect(Collectors.toList());
    }

    private BigDecimal getRate(Long num, Long totalNum) {
        return new BigDecimal(num).divide(new BigDecimal(totalNum), 2, RoundingMode.HALF_UP);
    }

    private List<String> getYearMonthList(LocalDate today, long pe) {
        List<String> yearMonths = new ArrayList<>();
        for (int i = 0; i <= pe; i++) {
            LocalDate localDate = today.plusMonths(i);
            String yearMonth = localDate.toString().substring(0, 7);
            yearMonths.add(yearMonth);
        }
        //倒序
        // Collections.reverse(yearMonths);
        return yearMonths;
    }
}
